
from django.forms import Form,ModelForm,ValidationError
from django import forms
from models import Studio
from api.pts import PTS
from models import TempModel


class StudioForm(ModelForm):
    error_css_class = 'error'
    required_css_class = 'required'
    class Meta:
        model = Studio
        exclude = ['last_save']
        # fields = ['logo','dsp','link_ios','link_gp','link_face','link_twit','email','policy','ga_key','fea_app','copyright','temp']
    
    
    def clean_temp(self):
        temp = self.cleaned_data.get('temp')
        if not temp:
            temp = TempModel.objects.all().first()
        return temp
            



class ChoiceForm(Form):
    """
    used for adding studio to APG
    """
    name=forms.ChoiceField()
    
    def __init__(self,*args, **kwargs):
        super(ChoiceForm,self).__init__(*args, **kwargs)
        
        self.fields['name'].choices=self.get_studio()
    
    def clean_name(self):
        name = self.cleaned_data.get('name')
        if Studio.objects.filter(name=name).exists():
            raise ValidationError('studio has exist')
        else:
            return name
    
    
    def get_studio(self):
        pts=PTS()
        total_studios=set(pts.get_studios())
        crt_studios= set([s.name for s in Studio.objects.all()])
        
        left_studios=list(total_studios-crt_studios)
        left_studios.sort()
        return [(s,s) for s in left_studios]
            
    